#pragma once

#include <util/factory.h>

namespace framework
{

class ComponentFactory : public util::ObjectFactory< QString >
{
public:
  typedef util::ObjectFactory< QString > TParent;

public:

  template< class T >
  T * Make(const TCreatorID & CreatorID_)
  {
    return TParent::Make< T >(CreatorID_);
  }

  template< class T >
  T * Make(const TCreatorID & CreatorID_, const QString & ComponentID_)
  {
    GRef<ComponentFactory> cf;

    assert(mComponentRegistry.find(ComponentID_) == mComponentRegistry.end());

    T * pComponent = TParent::Make< T >(CreatorID_);

    if (pComponent != 0)
      mComponentRegistry[ComponentID_] = pComponent;
      
    return pComponent;
  }

  template< class T >
  T * Get(const QString & ComponentID_) const
  {
    TComponentRegistry::const_iterator it = mComponentRegistry.find(ComponentID_);

    if (it == mComponentRegistry.end())
      return 0;

    return it.value();
  }

  QList<QString> GetComponentNames() const
  {
    return mComponentRegistry.keys();
  }

private:
  typedef QMap< QString, QObject * > TComponentRegistry;

  TComponentRegistry mComponentRegistry;
};

} // namesapce framework
